package com.muli.muwai.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muli.muwai.entity.IntfFinProductionCost;
import com.muli.muwai.exception.MyDefinedEnum;
import com.muli.muwai.mapper.ProductionCostMapper;
import com.muli.muwai.service.ProductionCostService;
import com.muli.muwai.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.print.Book;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProductionCostImpl extends ServiceImpl<ProductionCostMapper, IntfFinProductionCost> implements ProductionCostService {
    @Autowired
    ProductionCostMapper productionCostMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void productionCost(List<String[]> productionCost) throws Exception {

        List<IntfFinProductionCost> list = getIntfFinProductionCosts(productionCost);
        //定义长度集合
        List<Integer> num = new ArrayList<>();
        HashMap<String, Integer> map = new HashMap<>();
        //分被获取1-12月列的值 确认哪月份有数据 有则删除数据后导入 以长度判断哪个月有有效数据
        Set<Double> jan = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getJan()).collect(Collectors.toSet());
        Set<Double> feb = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getFeb()).collect(Collectors.toSet());
        Set<Double> mar = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getMar()).collect(Collectors.toSet());
        Set<Double> apr = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getApr()).collect(Collectors.toSet());
        Set<Double> may = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getMay()).collect(Collectors.toSet());
        Set<Double> jun = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getJun()).collect(Collectors.toSet());
        Set<Double> jul = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getJul()).collect(Collectors.toSet());
        Set<Double> aug = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getAug()).collect(Collectors.toSet());
        Set<Double> sep = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getSept()).collect(Collectors.toSet());
        Set<Double> oct = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getOct()).collect(Collectors.toSet());
        Set<Double> nov = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getNov()).collect(Collectors.toSet());
        Set<Double> dec = list.stream().map(IntfFinProductionCost -> IntfFinProductionCost.getDece()).collect(Collectors.toSet());
        //加入map
        map.put("jan", jan.size());
        map.put("feb", feb.size());
        map.put("mar", mar.size());
        map.put("apr", apr.size());
        map.put("may", may.size());
        map.put("jun", jun.size());
        map.put("jul", jul.size());
        map.put("aug", aug.size());
        map.put("sep", sep.size());
        map.put("oct", oct.size());
        map.put("nov", nov.size());
        map.put("dec", dec.size());
        //加入list
        num.add(jan.size());
        num.add(feb.size());
        num.add(mar.size());
        num.add(apr.size());
        num.add(may.size());
        num.add(jun.size());
        num.add(jul.size());
        num.add(aug.size());
        num.add(sep.size());
        num.add(oct.size());
        num.add(nov.size());
        num.add(dec.size());

        //获取最大值
        Integer max = Collections.max(num);
        //返回值代表有效数据所代表的月份
        String key = getKey(map, max);


        //判断其他月份是否有误写的数据 如果有抛出异常
        //怎么获取最大值之外的值
        //筛选
        Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Integer> entry = it.next();
            //System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            if (!entry.getKey().equals(key)) {
                if (entry.getValue() != 0) {
                    throw new Exception(MyDefinedEnum.ERROR_MONTHDAY.getMsg());
                }
            }
        }

        //要求为上传单月数据其他月份数据不得传 测试数据所有月都有数据,故而报错
        //测试请注掉
        switch (key) {
            case "jan":
                int jan1 = productionCostMapper.deleteJan();
                saveBatch(list);
                System.out.println(jan1);
                break;
            case "feb":
                int feb1 = productionCostMapper.deleteFeb();
                saveBatch(list);
                System.out.println(feb1);
                break;
            case "mar":
                int mar1 = productionCostMapper.deleteMar();
                saveBatch(list);
                System.out.println(mar1);
                break;
            case "apr":
                int apr1 = productionCostMapper.deleteApr();
                saveBatch(list);
                System.out.println(apr1);
                break;
            case "may":
                int may1 = productionCostMapper.deleteMay();
                saveBatch(list);
                System.out.println(may1);
                break;
            case "jun":
                int jun1 = productionCostMapper.deleteJun();
                saveBatch(list);
                System.out.println(jun1);
                break;
            case "jul":
                int jul1 = productionCostMapper.deleteJul();
                saveBatch(list);
                System.out.println(jul1);
                break;
            case "aug":
                int aug1 = productionCostMapper.deleteAug();
                saveBatch(list);
                System.out.println(aug1);
                break;
            case "sep":
                int sep1 = productionCostMapper.deleteSep();
                saveBatch(list);
                System.out.println(sep1);
                break;
            case "oct":
                int oct1 = productionCostMapper.deleteOct();
                saveBatch(list);
                System.out.println(oct1);
                break;
            case "nov":
                int nov1 = productionCostMapper.deleteNov();
                saveBatch(list);
                System.out.println(nov1);
                break;
            case "dec":
                int dec1 = productionCostMapper.deleteDec();
                saveBatch(list);
                System.out.println(dec1);
                break;
        }


        //saveBatch(list);

    }

    private List<IntfFinProductionCost> getIntfFinProductionCosts(List<String[]> productionCost) throws Exception {
        List<IntfFinProductionCost> list = new ArrayList<>();
        //记录出现错误列
        int num=0;
        for (String[] strings : productionCost) {
            IntfFinProductionCost entity = new IntfFinProductionCost();
            entity.setFiscalYear(strings[0]);
            entity.setFactory(strings[1]);
            entity.setLevel1(strings[2]);
            entity.setLevel2(strings[3]);
            entity.setLevel3(strings[4]);
            entity.setAmountType(strings[5]);
            try {
                num=7;
                if (strings[6].equals("")) {
                    entity.setJan(Double.valueOf(0));
                } else {
                    entity.setJan(Double.parseDouble(strings[6]));
                }
                num=8;
                //entity.setFeb(strings[7]);
                if (strings[7].equals("")) {
                    entity.setFeb(Double.valueOf(0));
                } else {
                    entity.setFeb(Double.parseDouble(strings[7]));
                }
                num=9;
                //entity.setMar(strings[8]);
                if (strings[8].equals("")) {
                    entity.setMar(Double.valueOf(0));
                } else {
                    entity.setMar(Double.parseDouble(strings[8]));
                }
                num=10;
                //entity.setApr(strings[9]);
                if (strings[9].equals("")) {
                    entity.setApr(Double.valueOf(0));
                } else {
                    entity.setApr(Double.parseDouble(strings[9]));
                }
                num=11;
                //entity.setMay(strings[10]);
                if (strings[10].equals("")) {
                    entity.setMay(Double.valueOf(0));
                } else {
                    entity.setMay(Double.parseDouble(strings[10]));
                }
                num=12;
                //entity.setJun(strings[11]);
                if (strings[11].equals("")) {
                    entity.setJun(Double.valueOf(0));
                } else {
                    entity.setJun(Double.parseDouble(strings[11]));
                }
                num=13;
                //entity.setJul(strings[12]);
                if (strings[12].equals("")) {
                    entity.setJul(Double.valueOf(0));
                } else {
                    entity.setJul(Double.parseDouble(strings[12]));
                }
                num=14;
                //entity.setAug(strings[13]);
                if (strings[13].equals("")) {
                    entity.setAug(Double.valueOf(0));
                } else {
                    entity.setAug(Double.parseDouble(strings[13]));
                }
                num=15;
                //entity.setSept(strings[14]);
                if (strings[14].equals("")) {
                    entity.setSept(Double.valueOf(0));
                } else {
                    entity.setSept(Double.parseDouble(strings[14]));
                }
                num=16;
                //entity.setOct(strings[15]);
                if (strings[15].equals("")) {
                    entity.setOct(Double.valueOf(0));
                } else {
                    entity.setOct(Double.parseDouble(strings[15]));
                }
                num=17;
                // entity.setNov(strings[16]);
                if (strings[16].equals("")) {
                    entity.setNov(Double.valueOf(0));
                } else {
                    entity.setNov(Double.parseDouble(strings[16]));
                }
                num=18;
                //entity.setDece(strings[17]);
                if (strings[17].equals("")) {
                    entity.setDece(Double.valueOf(0));
                } else {
                    entity.setDece(Double.parseDouble(strings[17]));
                }
            } catch (Exception e) {
                int size = list.size() + 2;
                throw new Exception("第" + size + "行,第" + num + "列,格式错误");
            }

           // Date date = DateUtils.parseString2Date(strings[18], "yy/MM/dd");
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
                Date date1 = sdf.parse(strings[18]);
                Timestamp timestamp1 = new Timestamp(date1.getTime());
                entity.setCreatedTime(timestamp1);
            } catch (ParseException e) {
                int size = list.size() + 2;
                throw new Exception("第" + size + "行,第" + 19 + "列,请设写成yyyy/m/d格式");
            }

            entity.setCreatedBy(strings[19]);

           // Date date1 = DateUtils.parseString2Date(strings[20], "yy/MM/dd");
            try {
                SimpleDateFormat sdf1 = new SimpleDateFormat("yy/MM/dd");
                Date date2 = sdf1.parse(strings[20]);
                Timestamp timestamp2 = new Timestamp(date2.getTime());
                entity.setUpdatedTime(timestamp2);
            } catch (ParseException e) {
                int size = list.size() + 2;
                throw new Exception("第" + size + "行,第" + 21 + "列,请设写成yyyy/m/d格式");
            }

            entity.setUpdatedBy(strings[21]);
            list.add(entity);
        }
        return list;
    }
    //根据最大值获取月份
    private String getKey(HashMap<String, Integer> map, Integer value) {
        String key = null;

        for (String getKey : map.keySet()) {
            if (map.get(getKey).equals(value)) {
                key = getKey;
            }
        }
        return key;
    }
}
